package com.menu.Service.Impl;

import com.menu.Mapper.AddressMapper;
import com.menu.Pojo.AddressVO;
import com.menu.Service.AddressService;
import com.menu.Utils.ResponseResult;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.Date;
import java.util.List;

@Service
public class AddressServiceImpl implements AddressService {

    @Resource
    private AddressMapper userAddressMapper;

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<AddressVO> queryAll(Integer userId) {
        return userAddressMapper.queryAll(userId);
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public ResponseResult addNewUserAddress(AddressVO addressBO) {
        List<AddressVO> addressList = this.queryAll(addressBO.getUserId());
        if (addressList == null || addressList.isEmpty() || addressList.size() == 0) {
            addressBO.setIsDefault(1);
        }
        if(addressBO.getIsDefault() == 1){
            for (AddressVO addressVo:
                 addressList) {
                userAddressMapper.updateToBeDefault(addressVo.getAddressId(),0);
            }
        }
        addressBO.setCreatedTime(new Date(System.currentTimeMillis()));
        addressBO.setUpdatedTime(new Date(System.currentTimeMillis()));
        if ( userAddressMapper.insert(addressBO) == 1){
            return ResponseResult.success("添加成功！");
        }
        return ResponseResult.error("添加失败！");
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public ResponseResult deleteUserAddress(Integer userId, Integer addressId) {
        AddressVO temp = userAddressMapper.select(addressId);
        if (temp == null || !temp.getUserId().equals(temp.getUserId())){
            return ResponseResult.error("该地址不存在！");
        }
        if (userAddressMapper.delete(addressId) == 1){
            return ResponseResult.success("删除成功！");
        }
        return ResponseResult.success("删除失败！！");
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public ResponseResult updateUserAddress(AddressVO addressBO) {
        AddressVO temp = userAddressMapper.select(addressBO.getAddressId());
        if (temp == null || !temp.getUserId().equals(addressBO.getUserId())){
            return ResponseResult.error("该地址不存在！");
        }
        addressBO.setUpdatedTime(new Date(System.currentTimeMillis()));
        if (addressBO.getIsDefault() == 1){
            List<AddressVO> list = queryAll(addressBO.getUserId());
            for (AddressVO addressBOTemp:
                 list) {
                if (addressBOTemp.getIsDefault() == 1 && !addressBO.getAddressId().equals(addressBOTemp.getAddressId())){
                    return ResponseResult.error("已有默认地址！");
                }
            }
        }
        if (userAddressMapper.updateByPrimaryKeySelective(addressBO) == 1){
            return ResponseResult.success("更新成功！");
        }
        return ResponseResult.success("更新失败！");
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public ResponseResult updateUserAddressToBeDefault(Integer userId, Integer addressId) {
        List<AddressVO> list = queryAll(userId);
        for (AddressVO addressBOTemp:
                list) {
           if (addressBOTemp.getIsDefault() == 1){
               userAddressMapper.updateToBeDefault(addressBOTemp.getAddressId(),0) ;
           }
        }
        if (userAddressMapper.updateToBeDefault(addressId,1) == 1){
            return ResponseResult.success("设置成功！");
        }
        return ResponseResult.error("设置失败！");
    }
}
